package com.mylib.libcore.utils;

import android.annotation.SuppressLint;
import android.content.res.Resources;

import com.orhanobut.logger.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * TimeFormate
 *
 * @author Administrator
 */
@SuppressLint("SimpleDateFormat")
public class TimeFormate {
    private static Calendar msgCalendar = null;
    private static SimpleDateFormat dayFormat = null;
    private static SimpleDateFormat dateFormat = null;
    private static SimpleDateFormat yearFormat = null;

    @SuppressLint("SimpleDateFormat")
    /**
     * judge time
     * @param dateOne
     * @param dateTwo
     * @return
     */
    public static boolean judgeTime(String dateOne, String dateTwo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        boolean a = false;
        try {
            a = format.parse(dateOne).getTime() > format.parse(dateTwo)
                    .getTime() ? true : false;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return a;
    }

    public static String getTime(long time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date(time));
    }

    public static long getTimeLong(String timeStr, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(timeStr).getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1L;
    }

    public static long getNowTimeLong() {
        return System.currentTimeMillis();
    }

    public static String getNowTimeString() {
        return System.currentTimeMillis() + "";
    }

    public static String getStrTime_ymd(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    public static String getStrTime_MDHM(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    public static String getStrTime_HM(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }


    public static String getTimeForDate(Date date, int style) {
        SimpleDateFormat formatter = null;
        if (date == null)
            return "is null";
        if (style == 0) {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (style == 1) {
            formatter = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
        } else if (style == 2) {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else if (style == 3) {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
        }
        return formatter.format(date);
    }

    public static String getTimeForDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    /**
     * 获取当前是白天还是夜晚
     * 以日出日落开始算，日出：白天开始，日落：晚上开始
     * 如果没传入  日出或者日落，就按照早上6点或者下午6点开始
     * <p>
     * 传入的  日出格式为：06:31  日出：19:16
     * 默认  日出：06:30  日落：18:30
     * 比较，先比较小时，在比较 分钟
     *
     * @return 0:白天  1：晚上
     */
    public static Integer getDayOrNight() {
        String sunUpTimeString = ACacheUtil.getSunupTime();
        String sunSetTimeString = ACacheUtil.getSunsetTime();
//        Log.e("husong", "-------->>sunUpTimeString="+sunUpTimeString+"  sunSetTimeString="+sunSetTimeString);
        int dayOrNight = 0;
        String currentTimeString = getHM(System.currentTimeMillis());
        int currentTime[] = getTime(currentTimeString);
        int sunUpTime[] = getTime(sunUpTimeString);
        int sunSetTime[] = getTime(sunSetTimeString);

        //对比当前和日出
        if (currentTime[0] < sunUpTime[0]) {//当前时间小于日出，代表晚上
            dayOrNight = 1;
        } else if (currentTime[0] == sunUpTime[0]) {//比如6=6,判断分钟
            if (currentTime[1] < sunUpTime[1]) {//当前时间分钟比日出小
                dayOrNight = 1;
            } else {//大于日出时间，就是白天，比如 06:31 >= 06:30
                dayOrNight = 0;
            }
        } else if (currentTime[0] > sunUpTime[0]) {
            if (currentTime[0] < sunSetTime[0]) {//比日落小
                dayOrNight = 0;
            } else if (currentTime[0] == sunSetTime[0]) {//06=06，判断分钟
                if (currentTime[1] < sunSetTime[1]) {//小，就是白天
                    dayOrNight = 0;
                } else {//等于或者大都是晚上
                    dayOrNight = 1;
                }
            } else {//比如 7>6  ，是晚上
                dayOrNight = 1;
            }
        }
        return dayOrNight;
    }

    /**
     * 时间戳格式化
     *
     * @param time
     * @return
     */
    public static String longFormatTime(long time) {
        //转换为日期
        Date date = new Date();
        date.setTime(time);
        if (isToday(date)) { //今天
            int minute = minutesAgo(time);
            if (minute < 60) {//1小时之内
                if (minute <= 1) {//一分钟之内，显示刚刚
                    return "刚刚";
                } else {
                    return minute + "分钟前";
                }
            } else {
                return hoursAgo(time) + "小时前";
            }
        } else {
            if (isYestYesterday(date)) {//昨天，显示昨天
                return "昨天 ";
            } else {
//                return dayAgo(time) + "天前";
                return getYMd2(time);
            }
        }
    }

    private static int dayAgo(long time) {
        return (int) ((System.currentTimeMillis() - time) / (60000) / 60 / 24);
    }

    private static boolean isYestYesterday(Date date) {
        Date now = new Date();
        return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() + 1 == now.getDate());
    }

    private static boolean isToday(Date date) {
        Date now = new Date();
        return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() == now.getDate());
    }

    private static int minutesAgo(long time) {
        return (int) ((System.currentTimeMillis() - time) / (60000));
    }

    private static int hoursAgo(long time) {
        return (int) ((System.currentTimeMillis() - time) / (60000) / 60);
    }


    // 时间戳格式转换
    public static String getChatTime(long timesamp) {
        try {
            String result = "";
            SimpleDateFormat sdf = new SimpleDateFormat("dd");
            Date today = new Date(System.currentTimeMillis());
            Date otherDay = new Date(timesamp);
            int temp = Integer.parseInt(sdf.format(today))
                    - Integer.parseInt(sdf.format(otherDay));
            switch (temp) {
                case 0:
                    result = "今天" + getHM(timesamp);
                    break;
                case 1:
                    result = "昨天" + getHM(timesamp);
                    break;
                default:
                    result = getLongTime(timesamp);
                    break;
            }
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getHM(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

    public static String getymd2(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date(time));
    }

    public static String getLongTime(long time) {
        Logger.i(" time = " + time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }

    public static String getYMd(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(new Date(time));
    }

    public static String getYMd2(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(time));
    }

    public static String getMouthAndDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        return format.format(new Date(time));
    }

    public static String getHMS(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

    public static String getDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("dd");
        return format.format(new Date(time));
    }

    public static String getMonth(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM");
        return format.format(new Date(time));
    }

    public static String getYear(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(new Date(time));
    }

    /**
     * 获取当年第几周
     *
     * @param time
     * @return
     */
    public static int getWeekOfYear(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(time);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取当年第几周
     *
     * @param
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    public static int distanceDayNum(long time) {
        return (int) Math.ceil((time - System.currentTimeMillis()) / 1000f / 60f / 60f / 24f);
    }

    // 时间戳格式转换
    public static String getMouthAndDay_(long timesamp) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        int temp = Integer.parseInt(sdf.format(today))
                - Integer.parseInt(sdf.format(otherDay));
        switch (temp) {
            case 0:
                result = getHMS(timesamp);
                break;
            default:
                result = getMouthAndDay(timesamp);
                break;
        }
        return result;
    }

    public static long StringTolongTime(String time, String formatStr) {
        long l = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        Date d = null;
        try {
            d = sdf.parse(time);
            l = d.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return l;
    }


    private static boolean isSameDay(Calendar now, Calendar msg) {
        int nowDay = now.get(Calendar.DAY_OF_YEAR);
        int msgDay = msg.get(Calendar.DAY_OF_YEAR);

        return nowDay == msgDay;
    }

    private static boolean isYesterDay(Calendar now, Calendar msg) {
        int nowDay = now.get(Calendar.DAY_OF_YEAR);
        int msgDay = msg.get(Calendar.DAY_OF_YEAR);

        return (nowDay - msgDay) == 1;
    }

    private static boolean isTheDayBeforeYesterDay(Calendar now, Calendar msg) {
        int nowDay = now.get(Calendar.DAY_OF_YEAR);
        int msgDay = msg.get(Calendar.DAY_OF_YEAR);

        return (nowDay - msgDay) == 2;
    }

    /**
     * 今天：true
     * 非今天:false
     */
    public static boolean isToday(long timesamp) {

        if (timesamp == -1L)
            return false;

        Calendar now = Calendar.getInstance();
        Calendar preCal = Calendar.getInstance();
        preCal.setTimeInMillis(timesamp);

        if (now.get(Calendar.YEAR) == preCal.get(Calendar.YEAR))
            return now.get(Calendar.DAY_OF_YEAR) == preCal.get(Calendar.DAY_OF_YEAR);
        return false;
    }

    /**
     * 是否过中午12点
     */
    public static boolean isNoonAfter() {
        int curHour = Integer.parseInt(getTime(System.currentTimeMillis(), "H"));

        return curHour > 12 ? true : false;
    }

    /**
     * 18点前为白天：true
     * 18点后为晚上:false
     */
    public static boolean isDay() {

        long curTime = System.currentTimeMillis();

        String nightTimeStr = getTime(curTime, "yyyy-MM-dd 18:00:00");
        long nightTime = getTimeLong(nightTimeStr, "yyyy-MM-dd HH:mm:ss");

        if (nightTime == -1L)
            return true;
        return curTime < nightTime;
    }

    /**
     * 18点为白天晚上分界点
     * 当前时间为晚上，设置下一刻：明天7点
     * 反之设置当天18点
     */
    public static long getDayChange() {
        Calendar now = Calendar.getInstance();

        String nightTimeStr = getTime(now.getTimeInMillis(), "yyyy-MM-dd 18:00:00");
        long nightTime = getTimeLong(nightTimeStr, "yyyy-MM-dd HH:mm:ss");
        Calendar nowNext = Calendar.getInstance();
        nowNext.setTimeInMillis(nightTime);

        if (now.getTimeInMillis() >= nowNext.getTimeInMillis()) {
            nowNext.add(Calendar.HOUR, 13);
        }

        return nowNext.getTimeInMillis() - System.currentTimeMillis();
    }

    // 将时间戳转为字符串
    public static String getStrTime(long cc_time) {
        String re_StrTime = null;
        long lcc_time = Long.valueOf(cc_time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        sdf = new SimpleDateFormat("HH:mm");

        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }


    public static String getWeek(long timeStamp) {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 获取指定日期转换成星期几
        if (mydate == 1) {
            week = "周日";
        } else if (mydate == 2) {
            week = "周一";
        } else if (mydate == 3) {
            week = "周二";
        } else if (mydate == 4) {
            week = "周三";
        } else if (mydate == 5) {
            week = "周四";
        } else if (mydate == 6) {
            week = "周五";
        } else if (mydate == 7) {
            week = "周六";
        }
        return week;
    }

    public static String getWeek2(long timeStamp) {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 获取指定日期转换成星期几
        if (mydate == 1) {
            week = "星期天";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;
    }


    /**
     * 获取周几
     *
     * @param cc_time
     * @return
     */
    public static String getStrTime_week(String cc_time) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        long lcc_time = Long.valueOf(cc_time);
        String re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        switch (re_StrTime) {
            case "星期一":
                re_StrTime = "周一";
                break;
            case "星期二":
                re_StrTime = "周二";
                break;
            case "星期三":
                re_StrTime = "周三";
                break;
            case "星期四":
                re_StrTime = "周四";
                break;
            case "星期五":
                re_StrTime = "周五";
                break;
            case "星期六":
                re_StrTime = "周六";
                break;
            case "星期日":
                re_StrTime = "周日";
                break;
        }
        return re_StrTime;
    }



    /**
     * 根据传入的 10:02   拆分为 10   和 02
     *
     * @param timeString
     * @return
     */
    public static int[] getTime(String timeString) {
        int time[] = new int[2];
        time[0] = Integer.parseInt(timeString.trim().split(":")[0]);
        time[1] = Integer.parseInt(timeString.trim().split(":")[1]);
        return time;
    }

    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldDate(int distanceDay, String fromat) {
        SimpleDateFormat dft = new SimpleDateFormat(fromat);
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    public static String getWeek(String pTime) {
        String Week = "星期";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {

            c.setTime(format.parse(pTime));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }
        return Week;
    }

    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    public static String timeStringToTimeString(String time, String format1, String format2) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format1);
            Date date = null;
            date = formatter.parse(time);
            return new SimpleDateFormat(format2).format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

}
